package org.noip.milhous;

import java.util.HashMap;

public class JavaSQLConversion
{
	/**
	 * The java to FIELD type mappings
	 */
	private final HashMap<String, FIELDTYPES> Java = new HashMap<String, FIELDTYPES>();

	/**
	 * The sql to FIELDTYPE mappings
	 */
	private final HashMap<String, FIELDTYPES> Sql = new HashMap<String, FIELDTYPES>();

	/**
	 * This constructor will create all the mappings needed
	 */
	public JavaSQLConversion()
		{
			Java.put("int", FIELDTYPES.INT);
			Java.put("java.lang.Integer", FIELDTYPES.INT);
			Java.put("java.lang.Boolean", FIELDTYPES.BOOL);
			Java.put("boolean", FIELDTYPES.BOOL);
			Java.put("java.lang.String", FIELDTYPES.STRING);
			Java.put("java.lang.Long", FIELDTYPES.LONG);
			Java.put("long", FIELDTYPES.LONG);
			Java.put("java.sql.Date", FIELDTYPES.Date);
			// BigDecimal
			Java.put("java.math.BigDecimal", FIELDTYPES.DECIMAL);
			Java.put("java.lang.Double", FIELDTYPES.DOUBLE);
			Java.put("double", FIELDTYPES.DOUBLE);
			Java.put("java.sql.Blob", FIELDTYPES.Blob);
			Java.put("java.sql.Timestamp", FIELDTYPES.TIMESTAMP);
			Sql.put("double", FIELDTYPES.DOUBLE);
			Sql.put("float", FIELDTYPES.DOUBLE);
			Sql.put("varchar", FIELDTYPES.STRING);
			Sql.put("int", FIELDTYPES.INT);
			Sql.put("mediumtext", FIELDTYPES.STRING);
			Sql.put("mediumint", FIELDTYPES.INT);
			Sql.put("timestamp", FIELDTYPES.TIMESTAMP);
			Sql.put("datetime", FIELDTYPES.TIMESTAMP);
			Sql.put("date", FIELDTYPES.Date);
			Sql.put("tinyint", FIELDTYPES.INT);
			Sql.put("smallint", FIELDTYPES.INT);
			Sql.put("bool", FIELDTYPES.BOOL);
			Sql.put("char", FIELDTYPES.STRING);
			Sql.put("bigint", FIELDTYPES.LONG);
			Sql.put("longblob", FIELDTYPES.Blob);
			Sql.put("longtext", FIELDTYPES.STRING);
			Sql.put("text", FIELDTYPES.STRING);
			Sql.put("double unsigned", FIELDTYPES.DOUBLE);
			Sql.put("time", FIELDTYPES.Date);
			Sql.put("decimal", FIELDTYPES.DECIMAL);
			Sql.put("bit", FIELDTYPES.BOOL);

		}

	public FIELDTYPES getJavaType(String javaName)
		{
			FIELDTYPES result = FIELDTYPES.UserDefined;
			if (Java.get(javaName) != null)
				{
					result = Java.get(javaName);
				}
			return result;
		}

	/**
	 * get the data base type given the column name
	 * 
	 * @param dbName
	 *           the type name in the db
	 * @param col
	 *           extra column info
	 * @return the correct Type
	 */
	public FIELDTYPES getDBType(String dbName, COLUMNS col)
		{
			FIELDTYPES type = Sql.get(dbName);
			if (col == null)
				{
					System.out.println("error");
				}
			if (type == null)
				{
					System.out.println("error");
				}
			if (type.equals(FIELDTYPES.INT) && col.COLUMN_TYPE.equals("tinyint(1)"))// the mysql hack for boolean
				{
					type = FIELDTYPES.BOOL;
				}

			return type;
		}
}
